
"""
    检查数据是否存在
"""


from rest_framework.views import APIView
from rest_framework import status
from utils.json_response import JsonResponse
from users.models import UserProfileModel, Menu, Permission, Organization, Role


class UserProfileModelCheckUsername(APIView):
    """
    检查用户名是否存在
    """

    def get(self, request, *args, **kwargs):
        username = request.GET.get('username')
        obj_username = UserProfileModel.objects.filter(username=username)
        if obj_username:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class UserProfileModelCheckMobile(APIView):
    """
    检查手机号是否存在
    """

    def get(self, request, *args, **kwargs):
        username = request.GET.get('username')
        mobile = request.GET.get('mobile')
        obj_username = UserProfileModel.objects.filter(username=username).first()
        if obj_username:
            if obj_username.mobile not in mobile:
                obj_mobile = UserProfileModel.objects.filter(mobile=mobile).first()
                if obj_mobile:
                    return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
            return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)
        else:
            obj_mobile = UserProfileModel.objects.filter(mobile=mobile).first()
            if obj_mobile:
                return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
            return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class MenuModelCheckName(APIView):
    """
    检查菜单名是否存在
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        obj_name = Menu.objects.filter(name=name)
        if obj_name:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class MenuModelCheckSort(APIView):
    """
    检查菜单序号是否存在
    """

    def get(self, request, *args, **kwargs):
        sort = request.GET.get('sort')
        obj_sort = Menu.objects.filter(sort=sort)
        if obj_sort:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class MenuModelCheckPid(APIView):
    """
    检查父级菜单是否等于菜单名
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        pid = request.GET.get('pid')
        if pid:
            obj_pid = Menu.objects.filter(id=pid).values('name').first()
            if obj_pid['name'] in name:
                return JsonResponse(code=200, exists=True, message='不能选择与当前菜单名相同的父级菜单', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class PermissionModelCheckName(APIView):
    """
    检查权限名是否存在
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        obj_name = Permission.objects.filter(name=name)
        if obj_name:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class PermissionModelCheckMethod(APIView):
    """
    检查权限方法是否存在
    """

    def get(self, request, *args, **kwargs):
        method = request.GET.get('method')
        obj_method = Permission.objects.filter(method=method)
        if obj_method:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class PermissionModelCheckPid(APIView):
    """
    检查父级权限是否等于权限名
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        pid = request.GET.get('pid')
        if pid:
            obj_pid = Permission.objects.filter(id=pid).values('name').first()
            if obj_pid['name'] in name:
                return JsonResponse(code=200, exists=True, message='不能选择与当前菜单名相同的父级菜单', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class OrganizationModelCheckName(APIView):
    """
    检查组织名称是否存在
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        obj_name = Organization.objects.filter(name=name)
        if obj_name:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class OrganizationModelCheckPid(APIView):
    """
    检查父级菜单是否等于组织名称
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        pid = request.GET.get('pid')
        if pid:
            obj_pid = Organization.objects.filter(id=pid).values('name').first()
            if obj_pid['name'] in name:
                return JsonResponse(code=200, exists=True, message='不能选择与当前菜单名相同的父级菜单', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)


class RoleModelCheckName(APIView):
    """
    检查角色名是否存在
    """

    def get(self, request, *args, **kwargs):
        name = request.GET.get('name')
        obj_name = Role.objects.filter(name=name)
        if obj_name:
            return JsonResponse(code=200, exists=True, message='已存在', status=status.HTTP_200_OK)
        return JsonResponse(code=200, exists=False, message='', status=status.HTTP_200_OK)
